$(function(){
	layui.use(['form','layer'], function(){
		var form = layui.form;
		form.render();  
		var layer = layui.layer;
		function find_menu(){
			$.ajax({
				type:"get",
				url:"http://localhost:8080/ssmService/index/selectEnumRoleMenu",
				async:"true",
				"success":function(result){
					console.log(result);
					build_menu_list(result);
				}
			});
		}
		
		function build_menu_list(result){
			var menuList=result.data
			$.each(menuList, function(index,item) {
				var parentDiv=$("<div></div>").addClass("layui-form-item");
				var smdiv=$("<div></div>").addClass("layui-input-block");
				var parentChencBox=$("<input type='checkbox' class='parentCheckBox' lay-filter='
				' value='"+item.code +"'title='"+item.name+"'>")
				smdiv.append(parentChencBox);
				var smCheckBoxAttr=item.roleValue.split(",");
				for(var i=0;i<smCheckBoxAttr.length;i++){
					var smCheckBox=$("<input type='checkbox'  lay-filter='dddd' title='"+smCheckBoxAttr[i]+"'>")
					smdiv.append(smCheckBox);
				}
				
				parentDiv.append(smdiv);
				$(".menu").append(parentDiv);
			});
			form.render();
			
		}
		form.on("checkbox(parentCheckBox)",function(data){
			if(data.elem.checked){
				$(this).nextAll("input[type='checkbox']").prop("checked",true);
				
			}else{
				$(this).nextAll("input[type='checkbox']").prop("checked",false);
			}
			form.render();
		});
		form.on("checkbox(dddd)",function(data){
			var sum=0;
			if(data.elem.checked){
				$(this).siblings("input[type='checkbox'][class='parentCheckBox']").prop("checked",true);
			}else{
				/*var a=$(this).siblings("input[type='checkbox'][lay-filter='dddd']");
				a.each(function() {
					if(data[i].is(":checked")){
						sum=1;
						alert(sum);
					}
				});*/
				//$(this).siblings("input[type='checkbox'][class='parentCheckBox']").prop("checked",false);
			}
			form.render();
		});
		
		$("#addRole").click(function(){
			add_role_menu();
			var token=uuid(16,16);
			$("#token").val(token);
			return false;
				
		});
		
		form.on("submit(addRole)",function(data){
			var params=data.filed;
			add_Role(params);
			
		});
		
		AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz
		
		function add_Role(params){
			$.ajax({
				type:"post",
				url:"http://localhost:8080/ssmService/role/insertRoleVo",
				async:true,
				data:params,
				"success":function(result){
					if(result.resultCode==0){
						layer.msg("添加成功",{
							icon:1,
							time:1000
						});
					}
					parent.location()
				}
			})
		}
		
		
		function add_role_menu(){
			var index=0;
			$(".parentChecBox").each(function(){
				if($(this).is(":checked"))
				$(this).attr("name","listRolePermission['"+index+"'].code");
				var value="";
				$(this).nextAll("input[class='childrenCheckBox']").each(function(){
					if($(this).is(":checked")){
						value+="1";
					}else{
						value+="0";
					}
				});
				var hidden=$("<input type='hidden' name='listRolePermission["+index+"].permission_value'>")
				$(this).before(hidden);
				index++;
				alert(index)
			});
			
		}
		
		find_menu();
	});
	
	
	
	
	

	
	function uuid(len, radix) {
		var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
		var uuid = [],
			i;
		radix = radix || chars.length;
	
		if(len) {
			// Compact form
			for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
		} else {
			// rfc4122, version 4 form
			var r;
	
			// rfc4122 requires these characters
			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
			uuid[14] = '4';
	
			// Fill in random data. At i==19 set the high bits of clock sequence as
			// per rfc4122, sec. 4.1.5
			for(i = 0; i < 36; i++) {
				if(!uuid[i]) {
					r = 0 | Math.random() * 16;
					uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
				}
			}
		}
});